package facade;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.jsp.tagext.TryCatchFinally;

import entidad.Huesped;


@Stateless
@LocalBean
public class HuespedFacade implements HuespedFacadeRemote {

	@PersistenceContext(unitName = "sgh001EJB")
	private EntityManager em;

    public HuespedFacade() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public List<Huesped> Listar() {
		Query query = em.createQuery("select h from Huesped h order by h.numeroDoc");		
		@SuppressWarnings("unchecked")
		List<Huesped> result = query.getResultList();
		return result;
	}

	@Override
	public void Save(Huesped objHuesped) {
		try {			
			em.persist(objHuesped);			
		} catch (Exception e) {			
		}
	}

	@Override
	public void Update(Huesped objHuesped) {
		try {			
			em.merge(objHuesped);			
		} catch (Exception e) {			
		}
	}

	@Override
	public void Delete(String id) {
		Huesped objHuesped = new Huesped();
		try {
			objHuesped = em.find(Huesped.class, id);
			if(objHuesped != null){				
				em.remove(objHuesped);				
			}			
		} catch (Exception e) {
			
		}	
	}

	@Override
	public Huesped Find(String id) {
		try {		
			return em.find(Huesped.class, id);			
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<Huesped> Filtrar(String id) {
		
		try {		
			Query query = em.createQuery("SELECT h  FROM  Huesped h WHERE lower(h.numeroDoc)like lower (:p1) or "+
								"lower(h.nombres)like lower (:p2) or lower(h.apellidos)like lower (:p3)or "+
								"lower(h.tipoDoc)like lower (:p4) or lower(h.trato)like lower (:p5) or "+
								"lower(h.tipo)like lower (:p6) or lower(h.pai.nombrePais)like lower (:p7) order by h.numeroDoc");		
			
			query.setParameter("p1", id+"%");
			query.setParameter("p2", id+"%");
			query.setParameter("p3", id+"%");
			query.setParameter("p4", id+"%");
			query.setParameter("p5", id+"%");
			query.setParameter("p6", id+"%");
			query.setParameter("p7", id+"%");
			@SuppressWarnings("unchecked")
			List<Huesped> result = query.getResultList();
			return result;	
		} catch (Exception e) {
			
			return null;
		}
	}

}
